import request from "@/utils/request";

const API_PATH = "/{{ module_name }}/{{ business_name|lower }}";

// 参考 demo.ts 的风格，提供标准的 CRUD 与导入/导出 API（TypeScript）
const {{ class_name }}API = {
  // 列表查询
  list{{ class_name }}(query: {{ class_name }}PageQuery) {
    return request<ApiResponse<PageResult<{{ class_name }}Table[]>>>({
      url: `${API_PATH}/list`,
      method: "get",
      params: query,
    });
  },

  // 详情查询
  detail{{ class_name }}(id: number) {
    return request<ApiResponse<{{ class_name }}Table>>({
      url: `${API_PATH}/detail/${id}`,
      method: "get",
    });
  },

  // 新增
  create{{ class_name }}(data: {{ class_name }}Form) {
    return request<ApiResponse>({
      url: `${API_PATH}/create`,
      method: "post",
      data,
    });
  },

  // 修改（带主键）
  update{{ class_name }}(id: number, data: {{ class_name }}Form) {
    return request<ApiResponse>({
      url: `${API_PATH}/update/${id}`,
      method: "put",
      data,
    });
  },

  // 删除（支持批量）
  delete{{ class_name }}(ids: number[]) {
    return request<ApiResponse>({
      url: `${API_PATH}/delete`,
      method: "delete",
      data: ids,
    });
  },

  // 导出
  export{{ class_name }}(query: {{ class_name }}PageQuery) {
    return request<Blob>({
      url: `${API_PATH}/export`,
      method: "post",
      data: query,
      responseType: "blob",
    });
  },

  // 下载导入模板
  downloadTemplate{{ class_name }}() {
    return request<Blob>({
      url: `${API_PATH}/download/template`,
      method: "post",
      responseType: "blob",
    });
  },

  // 导入
  import{{ class_name }}(data: FormData) {
    return request<ApiResponse>({
      url: `${API_PATH}/import`,
      method: "post",
      data,
      headers: { "Content-Type": "multipart/form-data" },
    });
  },
  // 批量启用/停用
  batchAvailable{{ class_name }}(body: { ids: number[]; status: boolean }) {
    return request<ApiResponse>({
      url: `${API_PATH}/available/setting`,
      method: "patch",
      data: body,
    });
  },
};

export default {{ class_name }}API;

// ------------------------------
// TS 类型声明
// ------------------------------

// 列表查询参数
export interface {{ class_name }}PageQuery extends PageQuery {
  {% for column in columns %}
  {% if column.is_query and column.column != "BETWEEN" %}
  {{ column.python_field }}?: {{
    'boolean' if ('status' in (column.python_field|lower)) or (column.html_type == 'radio')
    else 'number' if column.is_pk == '1'
    else 'string'
  }};
  {% endif %}
  {% endfor %}
  start_time?: string;
  end_time?: string;
}

// 列表查询结果
export interface {{ class_name }}Table {
  index?: number;
  {% for column in columns %}
  {{ column.python_field }}?: {{
    'boolean' if ('status' in (column.python_field|lower)) or (column.html_type == 'radio')
    else 'number' if column.is_pk == 1
    else 'string'
  }};
  {% endfor %}
  creator?: creatorType;
}

// 新增/修改/详情表单参数
export interface {{ class_name }}Form {
  {% for column in columns %}
  {% if (column.is_insert == 1 or column.is_edit == 1) and column.python_field not in ['creatorId', 'createdAt', 'updatedAt'] %}
  {{ column.python_field }}?: {{
    'boolean' if ('status' in (column.python_field|lower)) or (column.html_type == 'radio')
    else 'number' if column.is_pk == 1
    else 'string'
  }};
  {% endif %}
  {% endfor %}
}
